﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class YSKMManagerController : AdminBaseController
    {
        //
        // GET: /YSKManager/
        /// <summary>
        /// 预算科目管理
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_DM_YSKM", Order = 1)]
        [LogPageActionFilter("预算科目管理", Order = 2)]
        public ActionResult Index()
        {
            List<Model.DM_YSKM> codeNameList = new List<Model.DM_YSKM>();
            ViewData["blcode"] = true;
            BLL.DM_YSKM codeNameBll = new BLL.DM_YSKM();
            codeNameList = codeNameBll.GetModelList("");
            ViewData["TaxClassList"] = new BLL.TaxClass().GetModelList("");
            return View(codeNameList);
        }

        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult DM_YSKMListPartial()
        {
            BLL.DM_YSKM codeNameBll = new BLL.DM_YSKM();
            List<Model.DM_YSKM> codeNameList = new List<Model.DM_YSKM>();
            string taxClass = Request.Params["taxClass"];
            string YSKMMC = Request.Params["YSKMMC"];
            string YSKMDM = Request.Params["YSKMDM"];
            string strwhere = "1=1 ";
            if (taxClass != "")
            {
                strwhere += " and ZSXM_DM='" + taxClass + "'";
            }
            if (YSKMMC != "")
            {
                strwhere += " and YSKM_MC like '%" + YSKMMC + "%'";
            }
            if (YSKMDM != "")
            {
                strwhere += " and YSKM_DM like '%" + YSKMDM + "%'";
            }
            codeNameList = codeNameBll.GetModelList(strwhere);
            ViewData["blcode"] = true;
            return PartialView("DM_YSKMListPartial", codeNameList);
        }

        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeClsNo">编码类别编码 必须有具体值</param>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult DM_YSKMEdit(string YSKM_DM, string ZSXM_DM)
        {
            Model.DM_YSKM codeNameObj = new Model.DM_YSKM();
            ViewData["blcodeNo"] = true;
            BLL.TaxClass taxClassBLL = new BLL.TaxClass();
            ViewData["TaxClassList"] = taxClassBLL.GetModelList("");
            if (null != YSKM_DM)
            {
                YSKM_DM = Fetch.estimateString(YSKM_DM, CheckGetEnum.Safety);
                BLL.DM_YSKM codeNameBll = new BLL.DM_YSKM();
                codeNameObj = codeNameBll.GetModel(YSKM_DM, ZSXM_DM);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.DM_YSKM();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }
            return View(codeNameObj);
        }


        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <param name="codeClsNo">编码类别编码</param>
        /// <param name="isAdd">是否添加true添加，false修改</param>
        /// <returns>是否添加成功</returns>
        public string YSKMSave(string YSKM_DM, string ZSXM_DM, string YSKM_MC, string YSK_DM, string YSK_MC, string GS_YSKM_MC, bool isAdd)
        {
            string str = string.Empty;

            YSKM_DM = Fetch.estimateString(YSKM_DM, CheckGetEnum.Safety);
            ZSXM_DM = Fetch.estimateString(ZSXM_DM, CheckGetEnum.Safety);
            YSKM_MC = Fetch.estimateString(YSKM_MC, CheckGetEnum.Safety);
            YSK_DM = Fetch.estimateString(YSK_DM, CheckGetEnum.Safety);
            YSK_MC = Fetch.estimateString(YSK_MC, CheckGetEnum.Safety);
            GS_YSKM_MC = Fetch.estimateString(GS_YSKM_MC, CheckGetEnum.Safety);
            BLL.DM_YSKM codeNameBll = new BLL.DM_YSKM();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(YSKM_DM, ZSXM_DM))
                {
                    return "exists";
                }
            }

            Model.DM_YSKM codeNameObj = new Model.DM_YSKM();
            codeNameObj.YSKM_DM = YSKM_DM;
            codeNameObj.YSKM_MC = YSKM_MC;
            codeNameObj.YSK_DM = YSK_DM;
            codeNameObj.YSK_MC = YSK_MC;
            codeNameObj.ZSXM_DM = ZSXM_DM;
            codeNameObj.GS_YSKM_MC = GS_YSKM_MC;

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }


        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeClsNO">编码类别编码</param>
        /// <returns>是否删除成功</returns>
        public string DelYSKM(string YSKM_DM, string ZSXM_DM)
        {
            string str = string.Empty;
            if (null != YSKM_DM)
            {
                YSKM_DM = Fetch.estimateString(YSKM_DM, CheckGetEnum.Safety);
                BLL.DM_YSKM codeNameBll = new BLL.DM_YSKM();
                try
                {
                    codeNameBll.Delete(YSKM_DM, ZSXM_DM);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
    }
}
